<?php

namespace VueRender\Traits;


use VueRender\Support\Js;

trait HasModels
{
    /**
     * @param string $stateKey
     * @param mixed|null $default
     * @return $this
     */
    public function modelValue(string $stateKey, mixed $default = null):static
    {
        return $this->modelProp("modelValue",$stateKey, $default);
    }

    /**
     * @param string $prop
     * @param string $stateKey
     * @param mixed|null $default
     * @return $this
     */
    public function modelProp(string $prop, string $stateKey, mixed $default = null):static
    {
        $this->models[$prop] = Js::model($stateKey,$default);
        if ($prop=="modelValue"){
            $this->attrs("v-model","state.".$stateKey);
        }else{
            $this->attrs("v-model:$prop", "state.".$stateKey);
        }
        return $this->state($stateKey,$default);
    }

}
